package com.ys.peony.ui.adapter;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ys.peony.R;
import com.ys.peony.interfaces.MyItemClickListener;
import com.ys.peony.model.BusiFileModel;
import com.ys.peony.model.BusiTypeFileModel;
import com.ys.peony.utils.PixelUtils;

import java.util.List;

/**
 * 列表adapter
 */
public class ClientPicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<BusiTypeFileModel> mDatas;
    private Context mContext;
    private MyItemClickListener.ClientItemListener mClickListener;

    public ClientPicAdapter(Context context, List<BusiTypeFileModel> data, MyItemClickListener.ClientItemListener listener) {
        this.mDatas = data;
        this.mContext = context;
        this.mClickListener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.picshow_list_item, parent, false);
        return new ClientPicHolder(view, mContext, mClickListener);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((ClientPicHolder) holder).setData(mDatas.get(position).getFiles(), mDatas.get(position).getName().toString().trim());
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    static class ClientPicHolder extends RecyclerView.ViewHolder {
        private final static int ITEM_SPAN = 4;
        private RecyclerView mRecyclerView;
        private Context mContext;
        private MyItemClickListener.ClientItemListener mClickListener;

        public ClientPicHolder(View itemView, Context context, MyItemClickListener.ClientItemListener listener) {
            super(itemView);
            this.mContext = context;
            this.mClickListener = listener;
            mRecyclerView = (RecyclerView) itemView.findViewById(R.id.recyclerview1);
            mRecyclerView.addItemDecoration(new SpaceItemDecoration(PixelUtils.dp2px(4),ITEM_SPAN));
        }
        public void setData(List<BusiFileModel> files, String name) {
            mRecyclerView.setLayoutManager(new GridLayoutManager(mContext, ITEM_SPAN, LinearLayoutManager.VERTICAL, false));
            mRecyclerView.setAdapter(new ClientPicItemAdapter(files, name, mContext, mClickListener,getPosition()));
        }
    }
    static class SpaceItemDecoration extends RecyclerView.ItemDecoration {

        private int mSpace;
        private int mCount;

        public SpaceItemDecoration(int space,int count) {
            this.mSpace = space;
            this.mCount = count;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            if (parent.getChildLayoutPosition(view) < mCount) {
                outRect.top = 0;
            } else {
                outRect.top = mSpace;
            }
        }

    }
}
